package com.sacfa.commons.bo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import org.apache.log4j.Logger;

import com.sacfa.commons.beans.Banco;
import com.sacfa.commons.connection.PoolConexion;
import com.sacfa.commons.sql.BancoSQL;
import com.sacfa.commons.utilities.SACFAExceptions;

/**
 * @author Leizar Zar
 * @version 1.0
 * @created 11-ago-2013
 */
public class BancoBo {
	private static Logger log = Logger.getLogger(BancoBo.class);
	
	public void registrar(Banco banco){
		PreparedStatement stm = null;
		Connection conex = PoolConexion.getInstance().getConexion();
		
		try {
			PoolConexion.getInstance().beginTransaction(conex);
			
			stm = conex.prepareStatement(BancoSQL.INSERT);
			stm.setString(1, banco.getNombre());
			stm.executeUpdate();
			
			PoolConexion.getInstance().commit(conex);
			PoolConexion.getInstance().endTransaction(conex);
		} catch(SQLException ex) {
			log.info("Error SQL", ex);
			throw new SACFAExceptions("Error SQL", ex);
		} finally {
			PoolConexion.getInstance().endConection(conex);
		}
	}

	public Banco consultar(Banco banco){
		PreparedStatement stm = null;
		ResultSet rs = null;
		Connection conex = PoolConexion.getInstance().getConexion();
		
		try {
			stm = conex.prepareStatement(BancoSQL.SELECT_ONE);
			stm.setString(1, banco.getNombre());
			rs = stm.executeQuery();
			
			rs.next();
			banco.setId(rs.getInt(1));
			banco.setNombre(rs.getString(2));
		} catch(SQLException ex) {
			log.info("Error SQL", ex);
			throw new SACFAExceptions("Error SQL", ex);
		} finally {
			PoolConexion.getInstance().endConection(conex);
		}
		return banco;
	}
	
	public Collection<Banco> consultaGeneral(){
		Collection<Banco> bancos = new ArrayList<Banco>();
		Banco banco = new Banco();
		PreparedStatement stm = null;
		ResultSet rs = null;
		Connection conex = PoolConexion.getInstance().getConexion();
		
		try {
			stm = conex.prepareStatement(BancoSQL.SELECT_ALL);
			rs = stm.executeQuery();
			
			while(rs.next()){
				banco.setId(rs.getInt(1));
				banco.setNombre(rs.getString(2));
				bancos.add(banco);
			}
		} catch(SQLException ex) {
			log.info("Error SQL", ex);
			throw new SACFAExceptions("Error SQL", ex);
		} finally {
			PoolConexion.getInstance().endConection(conex);
		}
		return bancos;
	}
	
	public void modificar(Banco banco){
		PreparedStatement stm = null;
		Connection conex = PoolConexion.getInstance().getConexion();
		
		try {
			PoolConexion.getInstance().beginTransaction(conex);
			
			stm = conex.prepareStatement(BancoSQL.UPDATE);
			stm.setString(1, banco.getNombre());
			stm.setInt(2, banco.getId());
			stm.executeUpdate();
			
			PoolConexion.getInstance().commit(conex);
			PoolConexion.getInstance().endTransaction(conex);
		} catch(SQLException ex) {
			log.info("Error SQL", ex);
			throw new SACFAExceptions("Error SQL", ex);
		} finally {
			PoolConexion.getInstance().endConection(conex);
		}
	}
	
	public void eliminar(Banco banco){
		PreparedStatement stm = null;
		Connection conex = PoolConexion.getInstance().getConexion();
		
		try {
			PoolConexion.getInstance().beginTransaction(conex);
			
			stm = conex.prepareStatement(BancoSQL.DELETE);
			stm.setInt(1, banco.getId());
			stm.executeUpdate();
			
			PoolConexion.getInstance().commit(conex);
			PoolConexion.getInstance().endTransaction(conex);
		} catch(SQLException ex) {
			log.info("Error SQL", ex);
			throw new SACFAExceptions("Error SQL", ex);
		} finally {
			PoolConexion.getInstance().endConection(conex);
		}
	}
}
